#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""
    Part of Seagull - The Pyhon Pages engine
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    utils:
      Contains utility functions
"""
__author__ = 'Jonathan Slenders <jonathan at slenders dot be>'
__license__ = 'GNU LGPL'


import urllib

def split_query_string(query_string):
	"""
	Accepts an query string, and returns
	a mapping of the key/value pairs.
	Input string is 'str' and may look like 'Fran%c3%a7ois'
	Output is unicode dictionary
	"""
	assert(isinstance(query_string, str))

	if len(query_string) > 0:
		params = { }

		for p in query_string.split('&'):
			if p.find('=') >= 0:
				key, value = p.split('=', 1)
				key = urllib.unquote_plus(key).decode('utf-8')
				value = urllib.unquote_plus(value).decode('utf-8')

				params[key] = value
		return params

	return {}

def urlencode(url):
	"""
	Encodes special characters in URLs. This is unicode compatiple
	in contrast with the urllib implementation which is not.
	"""
	if isinstance(url, unicode):
		return urllib.urlencode( {'uri': url.encode('utf-8') } ).encode('utf-8')

	elif isinstance(url, str):
		return urllib.urlencode( {'uri': url} ).encode('utf-8')

	else:
		raise 'URL is not a string or unicode object'

def xml_escape(text):
	if isinstance(text, str):
		text = text.decode('utf-8')

	text = text.replace(u'&', u'&amp;')
	text = text.replace(u'<', u'&lt;')
	text = text.replace(u'>', u'&gt;')
	text = text.replace(u'"', u'&quot;')
	return text

		#import xml.sax.saxutils
		#text = text.encode('utf-8')
		#return xml.sax.saxutils.escape (text).encode('utf-8')

def status_code_message(code):
	return http_status_codes[code]

http_status_codes = {
	# See http://tools.ietf.org/html/rfc2616
	# and http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
		200: 'OK',
		201: 'Created',
		202: 'Accepted',
		203: 'Non-Authoritative Information',
		204: 'No Content',
		205: 'Reset Content',
		206: 'Partial Content',
		207: 'Multi-Status',

		300: 'Multiple Choices',
		301: 'Moved Permanently',
		302: 'Found',
		303: 'See Other',
		304: 'Not Modified',
		305: 'Use Proxy',
		306: 'Switch Proxy', # No longer used
		307: 'Temporary Redirect',

		400: 'Bad Request',
		401: 'Unauthorized', # Access denied
		402: 'Payment Required',
		403: 'Forbidden',
		404: 'Not Found',
		405: 'Method Not Allowed',
		406: 'Not Acceptable',
		407: 'Proxy Authentication Required',
		408: 'Request Timeout',
		409: 'Conflict',
		410: 'Gone',
		411: 'Length Required',
		412: 'Precondition Failed',
		413: 'Request Entity too large',
		414: 'Request-URI Too Long',
		415: 'Unsupported Media Type',
		416: 'Requested Range Not Satisfiable',
		417: 'Expectation Failed',
		422: 'Unprocessable Entity',
		423: 'Locked',
		424: 'Failed Dependency',
		425: 'Unordered Collection',
		426: 'Upgrade required', 
		449: 'Retry With',

		500: 'Internal Server Error',
		501: 'Not implemented',
		502: 'Bad Gateway',
		503: 'Service Unavailable',
		504: 'Gateway Timeout',
		505: 'HTTP Version Not Supported',
		506: 'Variant Also Negotiates',
		507: 'Insufficient Storage',
		509: 'Bandwidth Limit Exceeded',
		510: 'Not Extended',

		}
